/**
* @depends jquery.ui.js
*/

$(function() {

	$.widget( 'ui.action_user', {
	
		_init: function() {
		
			this._initActions();
			
			$('.SearchBar', this.element).widget_searchbar();
			
			$('.Contacts.Box', this.element).widget_box_contacts();
		},
		
		_initActions: function() {
		
			var user_id = $('.User.Panel', this.element).attr( 'user_id' );
		
			$('a[action=user_preferences]', this.element).click( function() {
				$.user.preferences( user_id, function() {
					location.reload();
				} );
			} );
			
			$('a[action=user_picture]', this.element).click( function() {
				$.user.picture( user_id, function() {
					location.reload();
				} );
			} );
			
			$('a[action=contact_add]', this.element).click( function() {
				$.contact.add( user_id, function() {
					location.reload();
				} );
			} );
			
			$('a[action=contact_answer]', this.element).click( function() {
				var request_id = $(this).closest( '[request_id]' ).attr( 'request_id' );
				$.contact.answer( request_id, function() {
					location.reload();
				} );
			} );
			
			$('a[action=contact_remove]', this.element).click( function() {
				$.contact.remove( user_id, function() {
					location.reload();
				} );
			} );
		
		}
	
	} );

	if( $('.User.Panel').length ) {
		$(document.body).action_user();
	}

});
